require("../util.js");
require("../component/components.js");
require("../component/cityChoice.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
var dataInfo=JSON.parse(sessionStorage.getItem("data"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#userEdit",
    data:{
        list:{
            loginUid:userInfo.id,
            nickname:"",   //昵称
            sex:"",  //string  性别 1男2女
            age:"",  //string  年龄
            education:"",//学历
            occupation:"",//职业
            // birthday:"",//生日
            height:"", //身高
            constellation:"",//星座
            monthlyIncome:"",//月收入
            weight:"",//体重
            located:"",//所在地
            marital:"",//情感状态
            hobby:""  //爱好
        },
        listName:{
            // uid:userInfo.id,
            // nickname:"",   //昵称
            // sex:"",  //string  性别 1男2女
            // age:"",  //string  性别 1男2女
            // education:"",//学历
            // occupation:"",//职业
            // birthday:"",//生日
            // height:"", //身高
            educationName:"",
            constellationName:"",//星座
            monthlyIncomeName:"",//月收入
            // weight:"",//体重
            // located:"",//所在地
            maritalName:""//情感状态
            // hobby:""  //爱好
        },
        message:"家乡",
        isScreen:"",
        dataInfo:dataInfo,
        userInfo:{},
        nowYear:"",
        nowMonth:"",
        nowDate:"",
        initId1:"",
        rCategorys:[],
        initId2:"",
        rCategorys1:[],

    },
    mounted:function(){
        this.getUserInfo();
    },
    methods:{
        //后退
        getBack: function () {
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取个人信息
        getUserInfo:function(){
            var that=this;
            var data={
                loginUid:that.list.loginUid
            };
            that.ajax({url:"/happyi/getUserInfo",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                  that.userInfo=b.json.data;
                  that.list.nickname=that.userInfo.nickname; //昵称
                  that.list.education=that.userInfo.education; //学历
                  that.listName.educationName=that.userInfo.educationName; //学历
                  that.list.monthlyIncome=that.userInfo.monthlyIncome;  //月收入
                  that.listName.monthlyIncomeName=that.userInfo.monthlyIncomeName;//月收入
                  that.list.marital=that.userInfo.marital;  //情感状态
                  that.listName.maritalName=that.userInfo.maritalName;//情感状态
                  that.list.constellation=that.userInfo.constellation;  //星座
                  that.listName.constellationName=that.userInfo.constellationName;//星座
                  that.list.located=that.userInfo.located;  //所在地
                  that.list.occupation=that.userInfo.occupation;  //职业
                  that.list.height=that.userInfo.height;  //身高
                  that.list.weight=that.userInfo.weight;  //体重
                  that.list.hobby=that.userInfo.hobby;    //爱好
                  that.list.sex=that.userInfo.sex;  //生日
                  that.list.age=that.userInfo.age;//年龄
                    console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("获取个人信息失败！");
                }
            });
        },
        getIsScreen:function(num){
            this.isScreen=num;
        },
        //下拉框选择
        getSelect:function(num){
            var that=this;
            var type;
            if(num==1){  //学历
                type="hk_recruit_education";
            }else if(num==2){   //月收入
                type="hk_recruit_salary";
            }else if(num==3){  //婚姻
                type="hk_user_marital";
            }else if(num==4){   //星座
                type="hk_user_constellation";
            }
            console.log(type)
            var arr={};
            var data=[];
             that.dataInfo.dict.forEach(function(val,key){
                 if(val.type==type){
                     arr={};
                     arr.id=val.id;
                     arr.value=val.label;
                     data.push(arr);
                }
              });
            var bankSelect = new IosSelect(1,
                [data],
                {
                    container: '.provingMode',
                    itemHeight: 30,
                    itemShowCount: 3,
                    callback: function (selectOneObj) {
                        console.log(selectOneObj)
                        if(num==1){  //学历
                            that.list.education=selectOneObj.id;
                            that.listName.educationName=selectOneObj.value;
                        }else if(num==2){   //月收入
                            that.list.monthlyIncome=selectOneObj.id;
                            that.listName.monthlyIncomeName=selectOneObj.value;
                        }else if(num==3){  //婚姻
                            that.list.marital=selectOneObj.id;
                            that.listName.maritalName=selectOneObj.value;
                        }else if(num==4){   //星座
                            that.list.constellation=selectOneObj.id;
                            that.listName.constellationName=selectOneObj.value;
                        }
                    }
                });
        },
        //下拉框选择其他（体重，年龄，身高，性别）
        getSelectOther:function(type){
            var that=this;
            var data=[],arr={},oneId;
            // var arr={};
            if(type==1){  //年龄
                for(var i=10;i<=90;i++){
                    arr={};
                    arr.id=i;
                    arr.value=i;
                    data.push(arr);
                }
                oneId=30;
            }else if(type==2){  //身高height
                for(var i=100;i<=200;i++){
                    arr={};
                    arr.id=i;
                    arr.value=i+"cm";
                    data.push(arr);
                }
                oneId=160;
            }else if(type==3){  //体重
                for(var i=30;i<=150;i++){
                    arr={};
                    arr.id=i;
                    arr.value=i+"kg";
                    data.push(arr);
                }
                oneId=50;
            }else if(type==4){
                data=[{id:"1",value:"男"},{id:"2",value:"女"}]
            }
            console.log(data)
            var bankSelect = new IosSelect(1,
                [data],
                {
                    container: '.provingMode',
                    oneLevelId: oneId,
                    itemHeight: 30,
                    itemShowCount: 3,
                    callback: function (selectOneObj) {
                        console.log(selectOneObj)
                        if(type==1){  //年龄
                            that.list.age=selectOneObj.id;
                        }else if(type==2){   //身高
                            that.list.height=selectOneObj.value;
                        }else if(type==3){  //体重
                            that.list.weight=selectOneObj.value;
                        }else if(type==4){
                            that.list.sex=selectOneObj.id;
                        }
                    }
                });
        },
        //城市选择
        choiceList:function(data){
            console.log("ddddddddddddd")
            console.log(data)
            this.list.located=data.areasId;
            this.userInfo.locatedName=data.areasName;
        },
        //职业选择
        getOccupation:function(name){
            this.list.occupation=name;
            this.userInfo.occupation=name;
            this.rCategorys=[];
            this.rCategorys1=[];
            this.initId1="";
            this.initId2="";
            this.getIsScreen();
        },
        //日期选择
        getData:function(){
            // 初始化时间
            var that=this;
            var now = new Date();
            that.nowYear = now.getFullYear();
            that.nowMonth = now.getMonth() + 1;
            that.nowDate = now.getDate();
            var iosSelect = new IosSelect(3,
                [yearData, monthData, dateData],
                {
                    // title: '地址选择',
                    itemHeight: 30,
                    oneLevelId: that.nowYear,
                    twoLevelId: that.nowMonth,
                    threeLevelId: that.nowDate ,
                    // showLoading: true,
                    callback: function (selectOneObj, selectTwoObj, selectThreeObj) {
                        //console.log(selectOneObj.value.replace(/[^0-9]/ig,"") + ' ' + selectTwoObj.value.replace(/[^0-9]/ig,"") + ' ' + selectThreeObj.value.replace(/[^0-9]/ig,""));
                        that.list.birthday=selectOneObj.value.replace(/[^0-9]/ig,"")+"-"+selectTwoObj.value.replace(/[^0-9]/ig,"")+"-"+selectThreeObj.value.replace(/[^0-9]/ig,"");
                        that.userInfo.birthday=that.list.birthday;
                        console.log(that.list.birthday)
                    }
                });
        },
        recruitCategorys:function(id,type){
            var that=this;
            if(type==1){
                that.rCategorys=[];
                that.rCategorys1=[];
                that.initId1=id;
            }else if(type==2){
                that.rCategorys1=[];
                that.initId2=id;
            }
            that.dataInfo.recruitCategorys.forEach(function(item,index){
                if(item.parentId==id){
                    if(type==1){
                        that.rCategorys.push(item);
                    }else if(type==2){
                        that.rCategorys1.push(item);
                    }
                }
            });
           // console.log(that.rCategorys);
        },
        //保存
        updateUserData:function(){
            var that=this;
            // var data={
            //     loginUid:that.list.uid
            // };
            console.log(that.list)
            that.ajax({url:"/happyi/updateUserData",data:that.list,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    userInfo.age=that.list.age;
                    userInfo.birthday=that.list.birthday;
                    // userInfo.name=that.list.nickname;
                    userInfo.occupation=that.list.occupation;
                    sessionStorage.setItem("userInfo",JSON.stringify(userInfo));
                    that.getBack();
                }else{
                    vm.$refs.prompt.toast("保存信息失败！");
                }
            });
        }
    }
    ,watch:{
        rCategorys:function(){
            this.$nextTick(function(){
                var that=this;
                var container = document.querySelectorAll('#screen .chooseCity-city');

                var expansion = null; //是否存在展开的list
                for(var i = 0; i < container.length; i++){
                    var x, y, X, Y, swipeX, swipeY;
                    container[i].addEventListener('touchstart', function(event) {
                        console.log(container)
                        x = event.changedTouches[0].pageX;
                        y = event.changedTouches[0].pageY;
                        swipeX = true;
                        swipeY = true ;
                        console.log("1111")
                        // if(vm.rCategorys){   //判断是否展开，如果展开则收起
                        //     expansion.className ="chooseCity-city clearfix";
                        // }
                    });
                    container[i].addEventListener('touchmove', function(event){
                        event.stopPropagation();
                        X = event.changedTouches[0].pageX;
                        Y = event.changedTouches[0].pageY;
                        // 左右滑动
                        if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
                            // 阻止事件冒泡
                            event.stopPropagation();
                            if(X - x > 10){   //右滑
                                // event.preventDefault();
                                this.className = "chooseCity-city clearfix";    //右滑收起
                            }
                            if(x - X > 10){   //左滑
                                event.preventDefault();
                                this.className = "chooseCity-city clearfix swipeleft";   //左滑展开
                                // expansion = this;
                            }
                            swipeY = false;
                        }
                        // 上下滑动
                        if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
                            swipeX = false;
                        }
                    });
                }
            });
        }
    }
});

// 数据初始化
var yearData = function(callback) {
    callback(formatYear(vm.nowYear))
};
var monthData = function (year, callback) {
    callback(formatMonth());
};
var dateData = function (year, month, callback) {
    if (/^(1|3|5|7|8|10|12)$/.test(month)) {
        callback(formatDate(31));
    }
    else if (/^(4|6|9|11)$/.test(month)) {
        callback(formatDate(30));
    }
    else if (/^2$/.test(month)) {
        if (year % 4 === 0 && year % 100 !==0 || year % 400 === 0) {
            callback(formatDate(29));
        }
        else {
            callback(formatDate(28));
        }
    }
    else {
        throw new Error('month is illegal');
    }
};
function formatYear (nowYear) {
    var arr = [];
    for (var i = nowYear - 45; i <= nowYear; i++) {
        arr.push({
            id: i + '',
            value: i + '年'
        });
    }
    return arr;
}
function formatMonth () {
    var arr = [];
    for (var i = 1; i <= 12; i++) {
        arr.push({
            id: i + '',
            value: i + '月'
        });
    }
    return arr;
}
function formatDate (count) {
    var arr = [];
    for (var i = 1; i <= count; i++) {
        arr.push({
            id: i + '',
            value: i + '日'
        });
    }
    return arr;
}